package com.gofun.framework.core.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.log4j.Logger;


public class FileUtils {

  private static final Logger logger = Logger.getLogger(FileUtils.class);

  /**
   * 
   * @Description : 读取文件转成字符串
   * @return : String
   * @Creation Date : 2016年4月26日 上午9:58:56
   * @Author : chutong
   */
  public static String readFileToString(String filePath) throws IOException {
    InputStreamReader in = null;
    InputStream inputStream = null;
    BufferedReader bufferedReader = null;
    String encoding = "UTF-8";
    StringBuffer sb = new StringBuffer();
    try {
      logger.info("read file" + filePath);
      inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
      in = new InputStreamReader(inputStream, encoding);
      bufferedReader = new BufferedReader(in);
      String lineTxt = null;
      while ((lineTxt = bufferedReader.readLine()) != null) {
        sb.append(lineTxt);
      }
    } catch (IOException e) {
      logger.error("读取文件" + filePath + "内容出错", e);
      throw e;
    } finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (IOException e) {
        logger.error("IO流关闭出错", e);
      }
    }
    return sb.toString();
  }

  /**
   * 根据文件（夹）路径强制删除
   * @author hbwhypw
   * @param path String 文件（夹）路径
   */
  public static void deleteAllFilesOfDir(String path) {
    if (path != null) {
      deleteAllFilesOfDir(new File(path));
    }
  }

  /**
   * 根据文件（夹）路径强制删除
   * @author hbwhypw
   * @param path File 文件（夹）路径
   */
  public static void deleteAllFilesOfDir(File path) {
    if (!path.exists())
      return;
    if (path.isFile()) {
      path.delete();
      return;
    }
    File[] files = path.listFiles();
    for (int i = 0; i < files.length; i++) {
      deleteAllFilesOfDir(files[i]);
    }
    path.delete();
  }

  /**
   * 压缩多个文件成zip压缩包
   * @author hbwhypw
   * @param dirPath 要被压缩的文件夹
   * @param zipFilePath 压缩后的文件存放路径
   * @return boolean true 压缩成功，false 压缩失败
   */
  public static boolean ZipMultiFile(String dirPath, String zipFilePath) {
    if (dirPath == null) {
      logger.error("参数 dirPath 不可以为空");
      return false;
    }
    if (zipFilePath == null) {
      logger.error("参数 zipFilePath 不可以为空");
      return false;
    }
    try {
      File file = new File(dirPath);// 要被压缩的文件夹
      File zipFile = new File(zipFilePath);//压缩后生成的zip文件
      InputStream input = null;
      ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
      if(file.exists() && file.isDirectory()){
        File[] files = file.listFiles();
        if (files != null) {
          for (File willZipFiles : files) {
            input = new FileInputStream(willZipFiles);
            zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + willZipFiles.getName()));
            int temp = 0;
            while ((temp = input.read()) != -1) {
              zipOut.write(temp);
            }
            input.close();
          }
        }
      } else {
        logger.error(dirPath + "文件夹不存在");
      }
      zipOut.close();
      return true;
    } catch (Exception e) {
      logger.error(e.getMessage());
      return false;
    }
  }
}
